<style lang='scss' scoped>
.user {
  .info {
    padding: 20px;
    display: flex;
    background-color: #fff;
    margin: 10px;
    border-radius: 5px;
    image {
      width: 100px;
      height: 100px;
    }
    .info-title {
      flex: 1;
      min-width: 0;
      margin-top: 10px;
      .name {
        font-size: 20px;
        margin-bottom: 10px;
      }
    }
    .arrow-right {
      font-size: 30px;
      font-weight: 300;
    }
  }
  .info-item {
    display: flex;
    background-color: #fff;
    padding: 10px;
    font-size: 18px;
    line-height: 28px;
    .item-title {
      flex: 1;
      min-width: 0;
      padding-left: 10px;
    }
  }
  .order {
    margin-top: 2px;
    background-color: #fff;
    padding: 0 10px;
    line-height: 24px;
    .order-title {
      padding: 10px 0;
      color: #999;
      display: flex;
      justify-content: space-between;
    }
    .order-content {
      .book-name {
        padding-bottom: 10px;
        font-size: 18px;
        font-weight: 400;
      }
    }
  }
}
</style>
<template>
  <view>
    <view class="u-page user">
      <div class="info" @click="toUserInfo">
        <img src="../../static/user-select.png" alt="" />
        <div class="info-title">
          <div class="name">{{ info.name }}</div>
          <div>{{ info.schoolName }}</div>
          <div style="color: #999">{{ info.collegeName }}</div>
        </div>
        <u-icon class="arrow-right" name="arrow-right"></u-icon>
      </div>
      <div class="info-item">
        <u-icon color="#57ABBE" class="icon" name="order"></u-icon>
        <div class="item-title">我的订单</div>
      </div>
      <div
        class="order"
        v-for="(order, index) in orderList"
        :key="index"
        @click="viewBookDetail(order)"
      >
        <div class="order-title">
          <span>创建时间 : {{ order.time }}</span>
          <span v-if="order.status === '0'">未审核</span>
          <span style="color: green" v-if="order.status === '1'"
            >已审核通过</span
          >
          <span style="color: red" v-if="order.status === '2'"
            >已审核未通过</span
          >
        </div>
        <div class="order-content">
          <div class="book-name">{{ order.bookName || '-' }}</div>
          <div v-for="(item, index) in order.orderBookInfos" :key="index">
            <div>{{ item.courseName }}</div>
            <span style="margin-right: 50px"
              >年级专业: {{ item.grade + '-' + item.majorName }}</span
            >
            <span>用书量: {{ item.bookNum || '-' }}</span>
          </div>
        </div>
        <div class="order-title">
          <span>教师用书数量 : {{ order.teacherBookNum || '-' }}</span>
          <span style="color: #57abbe"
            >学生总用书 : {{ order.bookNums }} 本</span
          >
        </div>
        <div style="color: red" v-if="order.status === '2'">
          审核不通过理由 : {{ order.remark }}
        </div>
      </div>
    </view>
  </view>
</template>

<script>
import dayjs from 'dayjs'
import * as api from '../../common/api.js'

export default {
  data() {
    return {
      info: {
        schoolName: '',
        collegeName: '',
        role: '',
        name: '',
        sex: ''
      },
      orderList: []
    }
  },
  onLoad() {
    uni.startPullDownRefresh()
  },
  async onPullDownRefresh() {
    try {
      await this.getUserInfo()
      await this.getOrderList()
    } catch (e) {
      console.log(e)
    } finally {
      wx.hideLoading()
      uni.stopPullDownRefresh()
    }
  },
  methods: {
    async getUserInfo() {
      this.info = await api.getTeacher()
      wx.setStorageSync('schoolId', this.info.schoolId)
    },
    async getOrderList() {
      const res = await api.getOrderList()
      this.orderList = res.map((item) => {
        item.time = item.createTime
          ? dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss')
          : '-'
        return item
      })
    },
    toUserInfo() {
      wx.navigateTo({
        url: `/pages/User/UserInfo?info=${JSON.stringify(this.info)}`
      })
    },
    viewBookDetail(item) {
      wx.navigateTo({
        url: `/pages/Home/ListDetail?orderList=${JSON.stringify(item)}`
      })
    }
  }
}
</script>